//
//  StatusCellOriginalView.swift
//  个人微博
//
//  Created by 一介布衣 on 16/2/21.
//  Copyright © 2016年 HUAMANLOU. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusCellOriginalView: UIView {

    // 添加约束对象属性，做记录使用
    var bottomConstraint: Constraint?
    
   // 添加模型属性
    var status: Status? {
        didSet {
            // 绑定数据
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)
            nameLabel.text = status?.user?.name
            mbrank_image.image = status?.user?.mbrank_image
            // TODO: 后期完善
//            timeLabel.text = status?.created_at
//            sourceLabel.text = status?.source
            textLabel.text = status?.text
            
            // 给配图视图的数组赋值
            pictureView.imageURLs = status?.imageURLs
            
            // 根据视图中是否有配图视图，然后动态调整约束关系，涉及到cell的复用,约束对象也会被复用
            // sstatus?.imageURLs永远不为空，如果没有配图视图会给定一个空数组

            // 解决约束冲突
            // 1.先卸载老的约束对象，使用属性记录底部的约束对象
            // 2.重新添加约束对象(重新添加约束时，使用make和update都是可以的)
            self.bottomConstraint?.uninstall()
            if let imageURLs = status?.imageURLs where imageURLs.count != 0 {
                // 有配图视图
                // 同一条约束存在(所相对的控件必须相同)就执行更新，不存在就执行添加约束
                self.snp_updateConstraints { (make) -> Void in
                     self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                }
            }else {
                // 没有配图视图
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(statusCellMargin).constraint
                })
            }
         }
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        self.backgroundColor = UIColor.yellowColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置UI
    private func setupUI() {
        // 添加子视图
        addSubview(sepView)
        addSubview(iconImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(mbrank_image)
        addSubview(verified_type_image)
        addSubview(textLabel)
        addSubview(pictureView)
        
        // 设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(8)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(statusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(statusCellMargin)
            // 约束头像的大小
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
            make.top.equalTo(iconImageView.snp_top)
        }
        
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(statusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(iconImageView.snp_right).offset(statusCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(statusCellMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        
        verified_type_image.snp_makeConstraints { (make) -> Void in
            // 右下角位置
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-statusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            //右边的约束不需要添加  在四张图片显示的时候 配图视图的布局有变化
            make.top.equalTo(textLabel.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(textLabel.snp_left)
            // 设置测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
         // 给原创微博底部添加约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
        // 将用户头像处理成圆角
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true
        
    }
    
    
    /// 懒加载视图
    // 头像
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    // 用户名
    private lazy var nameLabel:UILabel = UILabel(text: "iOS23期大神们", fontSize: 15, textColor: themeColor)
    
    // 微博发表时间
    private lazy var timeLabel:UILabel = UILabel(text: "11:11", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    // 微博来源
    private lazy var sourceLabel:UILabel = UILabel(text: "闲云悠鹤蝶恋舞", fontSize: 12, textColor: UIColor.lightGrayColor())
    
    // 微博等级
    private lazy var mbrank_image:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))

    // 用户认证类型
    private lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    // 微博正文
    private lazy var textLabel:UILabel = UILabel(text: "也无风雨也无晴", fontSize: 16, textColor: UIColor.darkGrayColor(), alignment: NSTextAlignment.Left)
    
    // 配图视图
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
    private lazy var sepView: UIView = {
       let sep = UIView()
        sep.backgroundColor = UIColor.redColor()
        return sep
    }()
}











